# -*- coding: utf-8 -*-
from ctrip_login_cookie import Ctrip
from configparser import ConfigParser
import datetime
import redis
cf = ConfigParser()
cf.read("config.ini")
# 导入各模块
from fcs import ota_back_hotel_room_type
from fcs import index,query_Market_Details
from fcs import query_Flow_Trans_Summary,query_Rank_Hotel,get_Traffic_Report
from fcs import get_Bidding_Request_Records
import pipelines
import datetime

host = cf.get("User-Redis-Database", "host")
port = cf.get("User-Redis-Database", "port")
password = cf.get("User-Redis-Database", "password")
db = cf.get("User-Redis-Database", "db")
r = redis.Redis(host=host, port=port, password=password, db=db, charset='utf-8',decode_responses=True)

# user='630317b68'
# password='tj653212'
# 登录获取cookie
# ck=Ctrip().login(user,password)
# print(ck)
# input()
#ck = {}

while True:
    datas = r.brpop('ctrip_shengyitong_users')[1]
    datas = eval(datas)
    print(datas)
    while True:
        CRS_ID = datas[0]
        user = datas[1]
        password = datas[2]
        # CRS_ID = 'CN_QUA056'
        # user = '28033399'
        # password = 'sj28033399.'
        print(CRS_ID,user,password)
        #登录获取cookie
        ck=Ctrip().login(user,password,CRS_ID)
        print(ck)
        # cks=''
        # for k,v in ck.items():
        #     cks+=(k+'='+v+';')
        # print(cks)
        # input('====')
        if ck==0 or ck=={} or ck=='':
            continue
        break
    if ck==1:
        continue
    item = {}
    item.update(CRS_ID=CRS_ID)
    # 获取主页信息酒店名酒店ID
    ck['CurrentLanguage']='SimpChinese'
    data=index.main(ck)
    hotel_id=data[1]
    hotel_name=data[0]
    print(hotel_id,hotel_name)

    # # 临时
    # r.lpush('ctrip_cookies', (hotel_name,hotel_id,ck))

    item.update(HotelId=hotel_id)
    item.update(HotelName=hotel_name)
    item.update(date=datetime.datetime.now().strftime('%Y-%m-%d'))

    flag=ota_back_hotel_room_type.main(ck,hotel_id,hotel_name)
    if flag==1:
        print(1)
        item.update(table='a_ota_back_basic')
        item.update(create_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        pipelines.process_item(item)
    else:
        da1=query_Market_Details.main(ck,hotel_id,hotel_name)
        item.update(da1)
        da2 = query_Flow_Trans_Summary.main(ck,hotel_id,hotel_name)
        item.update(da2)
        da3 = query_Rank_Hotel.main(ck,hotel_id,hotel_name)
        item.update(da3)
        da4 =get_Traffic_Report.main(ck,hotel_id,hotel_name)
        item.update(da4)
        get_Bidding_Request_Records.main(ck,hotel_id,hotel_name)
        # print(item)

        item.update(table='a_ota_back_basic')
        item.update(create_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        pipelines.process_item(item)



